package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysOrgDepartment;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.lab.*;
import com.ysd.lis.mapper.lab.LabReqUnqualifiedDetailMapper;
import com.ysd.lis.mapper.lab.LabReqUnqualifiedMapper;
import com.ysd.lis.request.lab.LabReqUnqualifiedDto;
import com.ysd.lis.request.lab.LabReqUnqualifiedRequest;
import com.ysd.lis.request.lab.LabReqUnqualifiedVo;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.service.lab.LabReqUnqualifiedService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 不合格原因处理表 服务实现类
 * </p>
 *
 * @author Bai
 * @since 2024-02-04
 */
@Service
public class LabReqUnqualifiedServiceImpl extends ServiceImpl<LabReqUnqualifiedMapper, LabReqUnqualified> implements LabReqUnqualifiedService {

    @Autowired
    private LabReqUnqualifiedMapper labReqUnqualifiedMapper;

    @Autowired
    SysBasDictDetailService sysBasDictDetailService;

    @Autowired
    LabReqUnqualifiedDetailMapper labReqUnqualifiedDetailMapper;

    @Override
    public Result getUnqualified(LabReqUnqualifiedRequest.GetUnqualifiedParams params) {
        List<LabReqUnqualified> labReqUnqualifieds = null;
        try {
            MPJLambdaWrapper<LabReqUnqualified> wrapper = new MPJLambdaWrapper<>();
            wrapper.selectAll(LabReqUnqualified.class);
            if (params.getSourceType().equals("samp")) {
                if (StringUtils.isNotBlank(params.getReqNo())) {
                    wrapper.eq(LabReqUnqualified::getReqNo, params.getReqNo());
                }
                wrapper.eq(LabReqUnqualified::getSampleda, params.getSampleda());
                wrapper.eq(LabReqUnqualified::getSampleNo, params.getSampleNo());
                wrapper.eq(LabReqUnqualified::getRptGroup, params.getRptGroup());
            } else {
                wrapper.eq(LabReqUnqualified::getReqNo, params.getReqNo());
                wrapper.isNull(LabReqUnqualified::getSampleda);
                wrapper.isNull(LabReqUnqualified::getSampleNo);
                wrapper.isNull(LabReqUnqualified::getRptGroup);
            }
            wrapper.eq(LabReqUnqualified::getType, 0);
            wrapper.eq(LabReqUnqualified::getDelFlag, 0);
            wrapper.orderByDesc(LabReqUnqualified::getOperatorTime);
            labReqUnqualifieds = labReqUnqualifiedMapper.selectList(wrapper);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return Result.succ(1, "查询成功", labReqUnqualifieds);
    }

    @Override
    public Result getUnqualifiedRecord(LabReqUnqualifiedDto labReqUnqualified) {
        MPJLambdaWrapper<LabReqUnqualified> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(LabReqUnqualified.class);
        queryWrapper.selectAs(LabReqMain::getPatNa, LabReqUnqualifiedVo::getPatNa);
        queryWrapper.selectAs(LabReqMain::getSex, LabReqUnqualifiedVo::getSex);
        queryWrapper.selectAs(LabReqMain::getAge, LabReqUnqualifiedVo::getAge);
        queryWrapper.selectAs(LabReqMain::getReqPurpose, LabReqUnqualifiedVo::getReqPurpose);
        queryWrapper.selectAs(LabReqMain::getTestPurpose, LabReqUnqualifiedVo::getTestPurpose);
        queryWrapper.selectAs(LabReqMain::getReqNo, LabReqUnqualifiedVo::getReqNo);
        queryWrapper.selectAs("dept", SysOrgDepartment::getName, LabReqUnqualifiedVo::getReqDeptNa);
        queryWrapper.leftJoin(LabReqMain.class, LabReqMain::getReqNo, LabReqUnqualified::getReqNo);
        queryWrapper.leftJoin(SysOrgDepartment.class, "dept", p -> p.eq(SysOrgDepartment::getCode, LabReqMain::getReqDept).eq(SysOrgDepartment::getDelFlag, 0).eq(SysOrgDepartment::getOrgId, LabReqMain::getOrgId).isNotNull(SysOrgDepartment::getCode));


        //申请记录
        if ("0".equals(labReqUnqualified.getType())) {
            queryWrapper.like(StringUtils.isNotBlank(labReqUnqualified.getReqNo()), LabReqMain::getReqNo, labReqUnqualified.getReqNo());
            queryWrapper.isNull(LabReqUnqualified::getSampleda);
            queryWrapper.isNull(LabReqUnqualified::getSampleNo);
            queryWrapper.isNull(LabReqUnqualified::getRptGroup);

            //标本记录
        } else {
            queryWrapper.like(StringUtils.isNotBlank(labReqUnqualified.getReqNo()), LabReqMain::getReqNo, labReqUnqualified.getReqNo());
            queryWrapper.like(StringUtils.isNotBlank(labReqUnqualified.getSampleNo()), LabMaininfo::getSampleNo, labReqUnqualified.getSampleNo());
            queryWrapper.isNotNull(LabReqUnqualified::getSampleda);
            queryWrapper.isNotNull(LabReqUnqualified::getSampleNo);
            queryWrapper.isNotNull(LabReqUnqualified::getRptGroup);
        }
        Timestamp startTime = Timestamp.valueOf(labReqUnqualified.getOperatorTimeStart().substring(0, 10) + " 00:00:00");
        Timestamp endTime = Timestamp.valueOf(labReqUnqualified.getOperatorTimeEnd().substring(0, 10) + " 23:59:59");
        queryWrapper.ge(LabReqUnqualified::getOperatorTime, startTime);
        queryWrapper.le(LabReqUnqualified::getOperatorTime, endTime);

        queryWrapper.like(StringUtils.isNotBlank(labReqUnqualified.getPatNa()), LabReqMain::getPatNa, labReqUnqualified.getPatNa());
        String reqDept = labReqUnqualified.getReqDept();
        if(ToolsUtils.isNotEmpty(reqDept)){
            queryWrapper.eq(LabReqMain::getReqDept, reqDept);
        }
        queryWrapper.eq(LabReqUnqualified::getType, 0);
        queryWrapper.eq(LabReqUnqualified::getDelFlag, 0);
        queryWrapper.orderByDesc(LabReqUnqualified::getOperatorTime);
        List<LabReqUnqualifiedVo> labReqUnqualifiedVos = labReqUnqualifiedMapper.selectJoinList(LabReqUnqualifiedVo.class, queryWrapper);
        return Result.succ(1, "查询成功", labReqUnqualifiedVos);
    }

    @Override
    public Result deleteUnqualified(LabReqUnqualified labReqUnqualified) {
        LambdaQueryWrapper<LabReqUnqualifiedDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LabReqUnqualifiedDetail::getReqUnqualifiedId, labReqUnqualified.getId());
        labReqUnqualifiedDetailMapper.delete(queryWrapper);
        labReqUnqualifiedMapper.deleteById(labReqUnqualified.getId());
        return Result.fail("删除失败");
    }

    @Override
    public Result saveUnqualified(LabReqUnqualified labReqUnqualified) {
        SysUser user = RedisUserManager.getUser();
        labReqUnqualified.setOrgId(user.getOrgId());
        labReqUnqualified.setType("0");
        labReqUnqualified.setOperator(user.getHisEmployeeCode());
        labReqUnqualified.setOperatorTime(new Date());
        Timestamp timestamp = new Timestamp(new Date().getTime());
        labReqUnqualified.setContactTime(timestamp);
        labReqUnqualifiedMapper.insert(labReqUnqualified);
        return Result.succ(1, "保存成功", null);
    }
}
